package com.whut.report;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.whut.mapper.NewPrefixSystemMapper;
import com.whut.model.*;
import com.whut.parMapper.ParProblemCollectDetailMapper;
import com.whut.parMapper.ParProblemDescriptionMapper;
import com.whut.parMapper.ParSystemMapper;
import com.whut.parMapper.utils.MapperUtils;
import com.whut.service.impl.CheckInfoServiceImpl;
import com.whut.service.impl.ReportServiceImpl;
import com.whut.utils.FileUtil;
import com.whut.utils.MDoc;
import com.whut.utils.pointsCalculate;
import com.whut.v_model.ProblemCollectDetail;
import com.whut.v_vo.ReducePoint;
import com.whut.v_vo.newHiddenReportGLList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.awt.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.whut.service.impl.CheckInfoServiceImpl.IMG_PATH;

public class NewHiddenCheckReport {

    @Resource
    private UrlProperties urlProperties;
    @Resource
    private Profiles profiles;
    @Resource
    private ParSystemMapper parSystemMapper;
    @Resource
    private ParProblemDescriptionMapper parProblemDescriptionMapper;
    @Resource
    private ParProblemCollectDetailMapper parProblemCollectDetailMapper;
    @Resource
    private CheckInfoServiceImpl checkInfoServiceImpl;
    // @Resource
    @Autowired
    private ReportServiceImpl reportServiceImpl;
    @Resource
    private MapperUtils mapperUtils;

    private int checkId;
    private int projectId;
    private boolean isNew;
    public static final int WIDTH = 718;
    public static final int HEIGHT = 468;
    public NewHiddenCheckReport(ParSystemMapper parSystemMapper,
                               ParProblemDescriptionMapper parProblemDescriptionMapper,
                               ParProblemCollectDetailMapper parProblemCollectDetailMapper,
                               CheckInfoServiceImpl checkInfoServiceImpl,
                               ReportServiceImpl reportServiceImpl,
                               MapperUtils mapperUtils,
                               UrlProperties urlProperties,
                               Profiles profiles) {
        this.parSystemMapper = parSystemMapper;
        this.parProblemDescriptionMapper = parProblemDescriptionMapper;
        this.parProblemCollectDetailMapper = parProblemCollectDetailMapper;
        this.checkInfoServiceImpl = checkInfoServiceImpl;
        this.reportServiceImpl = reportServiceImpl;
        this.mapperUtils=mapperUtils;
        this.urlProperties=urlProperties;
        this.profiles=profiles;

    }

    public String generateReport(int projectId, int checkId, boolean isNew) throws UnsupportedEncodingException {
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
        //应用主题样式
        DecimalFormat df = new DecimalFormat("0.00");
        int width = 718;
        int height = 468;
        ChartFactory.setChartTheme(standardChartTheme);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        Date date = new Date();
        DateFormat d1 = new SimpleDateFormat("yyyyMMddhhmmss");
        DateFormat d2 = new SimpleDateFormat("yyyy");
        DateFormat d3 = new SimpleDateFormat("MM");
        DateFormat d4 = new SimpleDateFormat("dd");
        String day = d4.format(date);
        String month = d3.format(date);
        String year = d2.format(date);
        String fileName = d1.format(date);
        if (parSystemMapper instanceof NewPrefixSystemMapper){
            System.out.println(true);
            String zlWeight = parSystemMapper.getWeight1ByName1("新质量检查");
            String aqWeight = parSystemMapper.getWeight1ByName1("新安全检查");
            String glWeight = parSystemMapper.getWeight1ByName1("新管理行为");
            dataMap.put("zlWeight", zlWeight);
            dataMap.put("aqWeight", aqWeight);
            dataMap.put("glWeight", glWeight);
        }else {
            String zlWeight = parSystemMapper.getWeight1ByName1("质量检查");
            String aqWeight = parSystemMapper.getWeight1ByName1("安全检查");
            String glWeight = parSystemMapper.getWeight1ByName1("管理行为");
            dataMap.put("zlWeight", zlWeight);
            dataMap.put("aqWeight", aqWeight);
            dataMap.put("glWeight", glWeight);
        }
        dataMap.put("day", day);
        dataMap.put("month", month);
        dataMap.put("year", year);
        dataMap.put("zlPoint", "\\");
        DefaultPieDataset datasetPie = new DefaultPieDataset();
        DefaultCategoryDataset datasetBar = new DefaultCategoryDataset();
        final String Bar = "各检查分项";
        //工程概况部分，来自ctp的工程信息
        reportServiceImpl.initProjectSummaryFirstPart(projectId, dataMap);
        //工程概况部分，来自设备信息表的设备信息部分
        java.util.List<Equipinfo> equipinfoList = mapperUtils.getEquipinfoMapper().getAllByProjectId(projectId);
        String equip = JSON.toJSONString(equipinfoList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
        JSONArray equipArray = JSONArray.parseArray(equip);
        java.util.List<Object> equipList = new ArrayList<Object>();
        if (equipArray.size() == 0) {
            Map<String, Object> eqMap = new HashMap<String, Object>();
            eqMap.put("equipType", "\\");
            eqMap.put("equipQua", "\\");
            eqMap.put("equipNum", "\\");
            eqMap.put("equipLeaseUnit", "\\");
            eqMap.put("equipUseUnit", "\\");
            eqMap.put("demolitionUnit", "\\");
            eqMap.put("testUnit", "\\");
            equipList.add(eqMap);
        } else {
            for (int i = 0; i < equipArray.size(); i++) {
                Map<String, Object> eqMap = new HashMap<String, Object>();
                JSONObject equipObject = equipArray.getJSONObject(i);
                eqMap.put("equipType", equipObject.getString("equipType"));
                eqMap.put("equipQua", equipObject.getString("equipQua"));
                eqMap.put("equipNum", equipObject.getString("equipNum"));
                eqMap.put("equipLeaseUnit", equipObject.getString("equipLeaseUnit"));
                eqMap.put("equipUseUnit", equipObject.getString("equipUseUnit"));
                eqMap.put("demolitionUnit", equipObject.getString("demolitionUnit"));
                eqMap.put("testUnit", equipObject.getString("testUnit"));
                equipList.add(eqMap);
            }
        }
        dataMap.put("equipList", equipList);


        //第一部分，（二）分项评估结果 改！
        java.util.List<Integer> pName1Info = parProblemCollectDetailMapper.getAllName1Id(checkId);
        String name1 = JSON.toJSONString(pName1Info, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
        JSONArray name1Array = JSONArray.parseArray(name1);   //firstId

        //安全文明评估结果
        java.util.List<Newfuzzyresult> fuzzyInfo = mapperUtils.getNewfuzzyresultMapper().getNewFuzzyInfoByCheckId(checkId, 4);
        java.util.List<Object> part2InnerList = new ArrayList<>();
        for (Newfuzzyresult nfz : fuzzyInfo) {
            Map<String, Object> part2InnerMap = new HashMap<>();
            String pName4 = parSystemMapper.getName4ByName4Id(nfz.getItemId());
            part2InnerMap.put("pName4", pName4);
            DecimalFormat df1 = new DecimalFormat("#.##");
            part2InnerMap.put("checkItemPoint", df1.format(nfz.getPoint()));
            String weight4 = "";
            switch (nfz.getResult()) {
                case 0:
                    weight4 = "可忽略风险";
                    break;
                case 1:
                    weight4 = "可容忍风险";
                    break;
                case 2:
                    weight4 = "可接受风险";
                    break;
                case 3:
                    weight4 = "不可接受风险";
                    break;
                case 4:
                    weight4 = "拒绝接受风险";
                    break;
            }
            part2InnerMap.put("weight4", weight4);
            part2InnerList.add(part2InnerMap);
        }
        java.util.List<Newfuzzyresult> newFuzzySumList = mapperUtils.getNewfuzzyresultMapper().getNewFuzzyInfoByCheckId(checkId, 1);
        dataMap.put("part2InnerList", part2InnerList);
        dataMap.put("aqPoint", "数据缺失");
        dataMap.put("weight4", "数据缺失");
        dataMap.put("checkItemPoint","");
        if(newFuzzySumList.size()!=0) {
            dataMap.put("aqPoint", newFuzzySumList.get(0).getPoint());
            dataMap.put("checkItemPoint", newFuzzySumList.get(0).getPoint());

            String aqWeight4 = "";
            switch (newFuzzySumList.get(0).getResult()) {
                case 0:
                    aqWeight4 = "可忽略风险";
                    break;
                case 1:
                    aqWeight4 = "可容忍风险";
                    break;
                case 2:
                    aqWeight4 = "可接受风险";
                    break;
                case 3:
                    aqWeight4 = "不可接受风险";
                    break;
                case 4:
                    aqWeight4 = "拒绝接受风险";
                    break;
            }
            dataMap.put("weight4", aqWeight4);

        }
            //安全管理评估结果
            java.util.List<newHiddenReportGLList> pName6Info = parProblemCollectDetailMapper.getAllGLInfoByCheckId(checkId);
            java.util.List<Object> part2glList = new ArrayList<>();
            int glIndex = 1;
            double glAllCheckPoint = 0.0;
            //有问题项
            for (newHiddenReportGLList nhr : pName6Info) {
                Map<String, Object> part2glMap = new HashMap<>();
                part2glMap.put("glIndex", glIndex);
                part2glMap.put("glPName6", nhr.getpName6());
                double glPoint6 = Double.parseDouble(nhr.getPoint6());
                for (ReducePoint rp : nhr.getReducePointList()) {
                    glPoint6 -= Double.parseDouble(rp.getReducePoint());
                }
                part2glMap.put("glPoint6", glPoint6 < 0 ? 0 : glPoint6);
                glAllCheckPoint += glPoint6 < 0 ? 0 : glPoint6;
                part2glList.add(part2glMap);
                glIndex++;
            }
            //无问题项
            java.util.List<UncheckSysPoint> noProblemList = mapperUtils.getUncheckSysPointMapper().getInfoByCheckId(checkId);
            for (UncheckSysPoint usp : noProblemList) {
                Map<String, Object> part2glMap = new HashMap<>();
                part2glMap.put("glIndex", glIndex);
                part2glMap.put("glPName6", usp.getpName6());
                part2glMap.put("glPoint6", usp.getPoint6());
                glIndex++;
                glAllCheckPoint += Double.parseDouble(usp.getPoint6());
                part2glList.add(part2glMap);
            }
            dataMap.put("glAllCheckPoint", glAllCheckPoint);
            dataMap.put("glIndexAll", glIndex);
            dataMap.put("glPoint", glAllCheckPoint);
            dataMap.put("part2glList", part2glList);
            //备注：综合评分=安全文明风险评分*90%+管理行为评分*10%
        dataMap.put("overAllPoint", "");

        if(newFuzzySumList.size()!=0) {

            double newOverAllPoint = newFuzzySumList.get(0).getPoint() * 0.9 + glAllCheckPoint * 0.1;

            dataMap.put("overAllPoint", newOverAllPoint);

        }
        //第一部分，（三）各个专业问题的分布情况
        java.util.List<Integer> pName4Info = parProblemCollectDetailMapper.getAllName4(checkId);
        int allProblemNum = parProblemCollectDetailMapper.getAllNum(checkId);
        dataMap.put("allProblemNum", allProblemNum);
        String name4 = JSON.toJSONString(pName4Info, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
        JSONArray name4Array = JSONArray.parseArray(name4);
        java.util.List<Object> part3List = new ArrayList<Object>();
        for (int m = 0; m < name4Array.size(); m++) {
            Map<String, Object> part3Map = new HashMap<String, Object>();
            int pName4Id = name4Array.getIntValue(m);
            String pName4 = parSystemMapper.getName4ByName4Id(pName4Id);
            int name4Num = parProblemCollectDetailMapper.countName4(pName4Id, checkId);
            String percent = pointsCalculate.caculateToPercent(name4Num, allProblemNum);
            part3Map.put("No", m + 1);
            part3Map.put("name4", pName4);
            part3Map.put("problemNum", name4Num);
            part3Map.put("percent", percent);
            datasetPie.setValue(pName4 + ":" + percent, new Double(name4Num));
            datasetBar.addValue(name4Num, Bar, pName4);
            part3List.add(part3Map);
        }
        dataMap.put("part3List", part3List);

        //第一部分，三.检查问题综述

        java.util.List<Object> partProblemList = new ArrayList<Object>();
        for (int i = 0; i < name1Array.size(); i++) {
            java.util.List<Object> partProblemName1List = new ArrayList<Object>();
            Map<String, Object> partProblemMap = new HashMap<String, Object>();
            String pName1 = parSystemMapper.getName4ByName4Id(name1Array.getIntValue(i));
            java.util.List<Integer> problemName4List = parProblemCollectDetailMapper.getAllName4ByName1(checkId, pName1);
            String problemName4Info = JSON.toJSONString(problemName4List, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
            JSONArray problemName4Array = JSONArray.parseArray(problemName4Info);
            for (int k = 0; k < problemName4Array.size(); k++) {
                java.util.List<ProblemCollectDetail> partProblemDetailList = parProblemCollectDetailMapper.getAllFourthInfo(checkId, problemName4Array.getIntValue(k));
                String partProblemDetailInfo = JSON.toJSONString(partProblemDetailList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                JSONArray partProblemDetailArray = JSONArray.parseArray(partProblemDetailInfo);
                String problemName4 = parSystemMapper.getName4ByName4Id(problemName4Array.getIntValue(k));
                for (int j = 0; j < partProblemDetailArray.size(); j++) {
                    Map<String, Object> partProblemName1Map = new HashMap<String, Object>();
                    JSONObject partProblemObject = partProblemDetailArray.getJSONObject(j);
                    String problemDescription = partProblemObject.getString("problemDescription");
                    String riskPoint = partProblemObject.getString("riskPoint");
                    if (partProblemObject.getString("isRemark").equals("N")) {
                        partProblemName1Map.put("problemDescription", problemDescription);
                    } else if (partProblemObject.getString("isRemark").equals("Y")) {
                        partProblemName1Map.put("problemDescription", partProblemObject.getString("remark"));
                    }
                    String riskPoint1 = "";
                    switch (riskPoint) {
                        case "提醒":
                            riskPoint1 = "提醒";
                            break;
                        case "1":
                            riskPoint1 = "四级隐患";
                            break;
                        case "2":
                            riskPoint1 = "三级隐患";
                            break;
                        case "3":
                            riskPoint1 = "二级隐患";
                            break;
                        case "4":
                            riskPoint1 = "一级隐患";
                            break;
                        default:
                            riskPoint1 = "";
                            break;
                    }
                    partProblemName1Map.put("riskPoint", riskPoint1);
                    partProblemName1Map.put("name4", problemName4);
                    partProblemName1List.add(partProblemName1Map);
                }
            }
            if (pName1.equals("安全检查")||pName1.equals("新安全检查")) {
                partProblemMap.put("name1", "安全");
            } else if (pName1.equals("质量检查")||pName1.equals("新质量检查")) {
                partProblemMap.put("name1", "质量");
            } else if (pName1.equals("管理行为")||pName1.equals("新管理行为")) {
                partProblemMap.put("name1", "管理行为");
            } else if (pName1.equals("新设备检查")) {
                    partProblemMap.put("name1", "新设备检查");
            }
            partProblemMap.put("partProblemName1List", partProblemName1List);
            partProblemList.add(partProblemMap);
        }
        dataMap.put("partProblemList", partProblemList);
        int txNum = parProblemCollectDetailMapper.getAllRiskPointCountByType(checkId, "提醒");
        int oneNum = parProblemCollectDetailMapper.getAllRiskPointCountByType(checkId, "1");
        int twoNum = parProblemCollectDetailMapper.getAllRiskPointCountByType(checkId, "2");
        int threeNum = parProblemCollectDetailMapper.getAllRiskPointCountByType(checkId, "3");
        int fourNum = parProblemCollectDetailMapper.getAllRiskPointCountByType(checkId, "4");
        dataMap.put("txNum", txNum);
        dataMap.put("oneNum", oneNum);
        dataMap.put("twoNum", twoNum);
        dataMap.put("threeNum", threeNum);
        dataMap.put("fourNum", fourNum);
        String txPercent = pointsCalculate.caculateToPercent(txNum, allProblemNum);
        String onePercent = pointsCalculate.caculateToPercent(oneNum, allProblemNum);
        String twoPercent = pointsCalculate.caculateToPercent(twoNum, allProblemNum);
        String threePercent = pointsCalculate.caculateToPercent(threeNum, allProblemNum);
        String fourPercent = pointsCalculate.caculateToPercent(fourNum, allProblemNum);
        String txAndOnePercent = pointsCalculate.caculateToPercent(txNum + oneNum, allProblemNum);
        dataMap.put("txPercent", txPercent);
        dataMap.put("onePercent", onePercent);
        dataMap.put("twoPercent", twoPercent);
        dataMap.put("threePercent", threePercent);
        dataMap.put("fourPercent", fourPercent);
        DefaultPieDataset datasetRiskPie = new DefaultPieDataset();
        DefaultCategoryDataset datasetRiskBar = new DefaultCategoryDataset();
        datasetRiskPie.setValue("四级隐患" + ":" + txAndOnePercent, new Double(txNum + oneNum));
        datasetRiskPie.setValue("三级隐患" + ":" + twoPercent, new Double(twoNum));
        datasetRiskPie.setValue("二级隐患" + ":" + threePercent, new Double(threeNum));
        datasetRiskPie.setValue("一级隐患" + ":" + fourPercent, new Double(fourNum));
        final String flag1 = "四级隐患";
        final String flag2 = "三级隐患";
        final String flag3 = "二级隐患";
        final String flag4 = "一级隐患";
        final String riskBar = "隐患等级情况统计";
        datasetRiskBar.addValue(txNum + oneNum, flag1, riskBar);
        datasetRiskBar.addValue(twoNum, flag2, riskBar);
        datasetRiskBar.addValue(threeNum, flag3, riskBar);
        datasetRiskBar.addValue(fourNum, flag4, riskBar);
        try {
            JFreeChart chart = ChartFactory.createPieChart("隐患分布饼图", datasetRiskPie, true, true, false);
            final PiePlot plot = (PiePlot) chart.getPlot();
            plot.setStartAngle(0);
            plot.setForegroundAlpha(0.90f);
            plot.setInteriorGap(0.02);
            chart.getLegend().setVisible(false);
            plot.setSectionPaint("四级隐患" + ":" + txAndOnePercent, new Color(49, 95, 232));
            plot.setSectionPaint("三级隐患" + ":" + twoPercent, new Color(232, 232, 49));
            plot.setSectionPaint("二级隐患" + ":" + threePercent, new Color(240, 117, 60));
            plot.setSectionPaint("一级隐患" + ":" + fourPercent, new Color(232, 36, 18));
            //File pieChart = new File("C:\\Users\\96500\\Desktop\\test\\" + fileName + "RiskpieChart.png");
            File pieChart = new File(IMG_PATH + fileName + "RiskpieChart.png");
            //File pieChart = new File("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "RiskpieChart.png");
            ChartUtils.saveChartAsPNG(pieChart, chart, width, height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            JFreeChart barChart = ChartFactory.createBarChart("隐患分布直方图",
                    "",
                    "数量",
                    datasetRiskBar,
                    PlotOrientation.VERTICAL,
                    true, true, false);
            CategoryPlot plot = (CategoryPlot) barChart.getPlot();
            plot.getRenderer().setSeriesPaint(0, new Color(49, 95, 232));
            plot.getRenderer().setSeriesPaint(1, new Color(232, 232, 49));
            plot.getRenderer().setSeriesPaint(2, new Color(240, 117, 60));
            plot.getRenderer().setSeriesPaint(3, new Color(232, 36, 18));
            BarRenderer barRenderer = (BarRenderer) plot.getRenderer();
            barRenderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
            barRenderer.setDefaultItemLabelsVisible(true);
            barRenderer.setItemLabelAnchorOffset(-20D);
            //File barChartFile = new File("C:\\Users\\96500\\Desktop\\test\\" + fileName + "RiskbarChart.png");
            File barChartFile = new File(IMG_PATH + fileName + "RiskbarChart.png");
            //File barChartFile = new File("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "RiskbarChart.png");

            ChartUtils.saveChartAsPNG(barChartFile, barChart, width, height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //dataMap.put("pic8", MDoc.getImageStr("C:\\Users\\96500\\Desktop\\test\\" + fileName + "RiskpieChart.png"));
        //dataMap.put("pic9", MDoc.getImageStr("C:\\Users\\96500\\Desktop\\test\\" + fileName + "RiskbarChart.png"));
        dataMap.put("pic8", MDoc.getImageStr(IMG_PATH + fileName + "RiskpieChart.png"));
        dataMap.put("pic9", MDoc.getImageStr(IMG_PATH + fileName + "RiskbarChart.png"));
        //dataMap.put("pic8", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "RiskpieChart.png"));
        //dataMap.put("pic9", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "RiskbarChart.png"));


        //第二部分，各部分工程检查情况
        String section2pName1 = "";
        java.util.List<Object> section2List = new ArrayList<Object>();
        for (int b = 0; b < name1Array.size(); b++) {
            Map<String, Object> section2Name1Map = new HashMap<String, Object>();
            java.util.List<Object> section2name1List = new ArrayList<Object>();
            String section2name1 = parSystemMapper.getName4ByName4Id(name1Array.getIntValue(b));
            if (section2name1.equals("安全检查")||section2name1.equals("新安全检查")) {
                section2pName1 = "安全问题";
            } else if (section2name1.equals("质量检查")||section2name1.equals("新质量检查")) {
                section2pName1 = "质量问题";
            } else if (section2name1.equals("管理行为")||section2name1.equals("新管理行为")) {
                section2pName1 = "管理体系及管理动作";
            }else if (section2name1.equals("新设备检查")) {
                section2pName1 = "新设备检查";

            }
            section2Name1Map.put("section2pName1", section2pName1);
            java.util.List<Integer> section2Name4Info = parProblemCollectDetailMapper.getAllName4ByName1(checkId, section2name1);
            String section2Name44 = JSON.toJSONString(section2Name4Info, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
            JSONArray section2Name4Array = JSONArray.parseArray(section2Name44);
            for (int w = 0; w < section2Name4Array.size(); w++) {
                Map<String, Object> section2Name4Map = new HashMap<String, Object>();
                java.util.List<Object> section2Name4List = new ArrayList<Object>();
                int section2Name4Id = section2Name4Array.getIntValue(w);
                String section2Name4 = parSystemMapper.getName4ByName4Id(section2Name4Id);
                java.util.List<ProblemCollectDetail> section2Name6Info = parProblemCollectDetailMapper.getAllName6InfoByName4(checkId, section2Name4Id, section2name1);
                String section2Name66 = JSON.toJSONString(section2Name6Info, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                JSONArray section2Name6Array = JSONArray.parseArray(section2Name66);
                for (int c = 0; c < section2Name6Array.size(); c++) {
                    Map<String, Object> section2Name6Map = new HashMap<String, Object>();
                    List<Object> picList = new ArrayList<Object>();
                    JSONObject section2Name6Object = section2Name6Array.getJSONObject(c);
                    section2Name6Map.put("section2pName6No", c + 1);
                    section2Name6Map.put("section2pName6", section2Name6Object.getString("pName6"));
                    section2Name6Map.put("section2pName5", section2Name6Object.getString("pName5"));
                    String section2RiskLevel = "";
                    switch (section2Name6Object.getString("riskPoint")) {
                        case "提醒":
                            section2RiskLevel = "提醒";
                            break;
                        case "1":
                            section2RiskLevel = "四级隐患";
                            break;
                        case "2":
                            section2RiskLevel = "三级隐患";
                            break;
                        case "3":
                            section2RiskLevel = "二级隐患";
                            break;
                        case "4":
                            section2RiskLevel = "一级隐患";
                            break;
                        default:
                            section2RiskLevel = "";
                            break;
                    }
                    section2Name6Map.put("section2RiskLevel", section2RiskLevel);
                    section2Name6Map.put("section2CheckPart", section2Name6Object.getString("checkPart"));
                    section2Name6Map.put("section2ProblemNum", section2Name6Object.getString("problemQua"));
                    if (section2Name6Object.getString("isRemark").equals("N")) {
                        section2Name6Map.put("section2problemDescription", section2Name6Object.getString("problemDescription"));
                    } else if (section2Name6Object.getString("isRemark").equals("Y")) {
                        section2Name6Map.put("section2problemDescription", section2Name6Object.getString("remark"));
                    }
                    section2Name6Map.put("section2normsProvison", section2Name6Object.getString("normsProvison"));
                    section2Name6Map.put("section2suggestion", section2Name6Object.getString("suggestion"));
                    String section2picPath = section2Name6Object.getString("picPath");
                    String picName = section2Name6Object.getString("picName");
                    JSONArray ja = JSONArray.parseArray(picName);
                    if (ja.size() == 0) {
                        //String s = MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\123.png");
                        String s = MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png");
                        picList.add(s);
                    } else {
                        for (int o = 0; o < ja.size(); o++) {
                            String s = "";
                            if ("prod".equals(profiles.getActives())) {
                                s = MDoc.getImageStr(section2picPath + ja.getString(o));
                            }
                            picList.add(s);
                        }
                    }
                    section2Name6Map.put("picList", picList);
                    section2Name4List.add(section2Name6Map);

                }
                section2Name4Map.put("section2pName4", section2Name4);
                section2Name4Map.put("section2Name4List", section2Name4List);
                section2name1List.add(section2Name4Map);
            }
            section2Name1Map.put("section2Name1List", section2name1List);
            section2List.add(section2Name1Map);
        }
        try {
            JFreeChart chart = ChartFactory.createPieChart("现场施工问题分布饼图", datasetPie, true, true, false);
            final PiePlot plot = (PiePlot) chart.getPlot();
            plot.setStartAngle(270);
            plot.setForegroundAlpha(0.90f);
            plot.setInteriorGap(0.02);
            chart.getLegend().setVisible(false);
            //File pieChart = new File("C:\\Users\\96500\\Desktop\\test\\" + fileName + "pieChart.png");
            File pieChart = new File(IMG_PATH + fileName + "pieChart.png");
            //File pieChart = new File("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "pieChart.png");
            ChartUtils.saveChartAsPNG(pieChart, chart, width, height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            JFreeChart barChart = ChartFactory.createBarChart("现场施工问题分布直方图",
                    "",
                    "数量",
                    datasetBar,
                    PlotOrientation.VERTICAL,
                    true, true, false);
            CategoryPlot plot = (CategoryPlot) barChart.getPlot();
            plot.getRenderer().setSeriesPaint(0, new Color(5, 87, 232));
            BarRenderer barRenderer = (BarRenderer) plot.getRenderer();
            barRenderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
            barRenderer.setDefaultItemLabelsVisible(true);
            barRenderer.setItemLabelAnchorOffset(-20D);
            barRenderer.setMaximumBarWidth(0.2);
            CategoryAxis categoryAxis = plot.getDomainAxis();
            categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
            //File barChartFile = new File("C:\\Users\\96500\\Desktop\\test\\" + fileName + "barChart.png");
            File barChartFile = new File(IMG_PATH + fileName + "barChart.png");
            //File barChartFile = new File("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "barChart.png");

            ChartUtils.saveChartAsPNG(barChartFile, barChart, width, height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //dataMap.put("pic6", MDoc.getImageStr("C:\\Users\\96500\\Desktop\\test\\" + fileName + "pieChart.png"));
        //dataMap.put("pic7", MDoc.getImageStr("C:\\Users\\96500\\Desktop\\test\\" + fileName + "barChart.png"));
        dataMap.put("pic6", MDoc.getImageStr(IMG_PATH + fileName + "pieChart.png"));
        dataMap.put("pic7", MDoc.getImageStr(IMG_PATH + fileName + "barChart.png"));
        //dataMap.put("pic6", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "pieChart.png"));
        //dataMap.put("pic7", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "barChart.png"));


        dataMap.put("section2List", section2List);
        MDoc mdoc = new MDoc();
      /*  if (reportType == 1) {
            mdoc.createDoc(dataMap, "C:\\Users\\96500\\Desktop\\test\\" + fileName + ".doc", "ftl\\5.0.ftl");
            MDoc.docToDocx("C:\\Users\\96500\\Desktop\\test\\" + fileName + ".doc", "C:\\Users\\96500\\Desktop\\test\\" + fileName + ".doc");
        } else if (reportType == 2) {
            mdoc.createDoc(dataMap, "C:\\Users\\96500\\Desktop\\Risktest\\" + fileName + ".doc", "ftl\\Risk2.0.ftl");
            MDoc.docToDocx("C:\\Users\\96500\\Desktop\\Risktest\\" + fileName + ".doc", "C:\\Users\\96500\\Desktop\\Risktest\\" + fileName + ".doc");
        }
        return "success";*/
        String path = FileUtil.getServerFolderPath();
        File f1 = new File(path, "doc");
        if (!f1.exists()) {
            f1.mkdirs();
        }
        String result = "";

        //mdoc.createDoc(dataMap, "E:\\tencent_File\\1296928795\\FileRecv\\yirui" + dataMap.get("projectName").toString().replace('#','号')+"小结报告"+fileName + ".doc", "ftl\\NewRisk2.0.ftl");
        //MDoc.docToDocx("E:\\tencent_File\\1296928795\\FileRecv\\yirui" + dataMap.get("projectName").toString().replace('#','号')+"小结报告"+fileName + ".doc", "E:\\tencent_File\\1296928795\\FileRecv\\yirui" + dataMap.get("projectName").toString().replace('#','号')+"小结报告"+fileName + ".doc");
        //result = "E:\\tencent_File\\1296928795\\FileRecv\\yirui" + dataMap.get("projectName").toString().replace('#','号')+"小结报告"+fileName + ".doc";
        mdoc.createDoc(dataMap, path + "\\doc\\" + dataMap.get("projectName").toString().replace('#', '号') + "安全风险报告" + fileName + ".doc", "ftl\\NewRisk2.0.ftl");
        //MDoc.docToDocx("D:\\whutServer\\apache-tomcat-8.5.41\\webapps\\ROOT\\doc\\" + dataMap.get("projectName").toString().replace('#','号')+"安全风险报告"+fileName + ".doc", "D:\\whutServer\\apache-tomcat-8.5.41\\webapps\\ROOT\\doc\\"+ dataMap.get("projectName").toString().replace('#','号')+"安全风险报告"+fileName +".doc");
        result = urlProperties.getAddress() + "/doc/" + dataMap.get("projectName").toString().replace('#', '号') + "安全风险报告" + fileName + ".doc";
//           mdoc.createDoc(dataMap, "D:\\WAAA\\新建文件夹" + "\\" + dataMap.get("projectName").toString().replace('#','号')+"隐患报告"+fileName + ".doc", "ftl\\Risk2.0.ftl");
//           MDoc.docToDocx("D:\\\\WAAA\\\\新建文件夹\\" + dataMap.get("projectName").toString().replace('#','号')+"隐患报告"+fileName + ".doc", "D:\\whutServer\\apache-tomcat-8.5.41\\webapps\\ROOT\\doc\\"+ dataMap.get("projectName").toString().replace('#','号')+"隐患报告"+fileName +".doc");

        return result;
    }
}
